{extend name="platform/base" /}
{block name="resources"}
<style>
	.left-indent:before{
		content:'';
		display:inline-block;
		width:20%;
	}
	.form-horizontal .form-group {
    margin-right: 0px;
    margin-left: 0px; 
	}
</style>
{/block}
{block name="main"}
<div class="row padder-v">
	<div class="col-sm-12">
		<a href="{:__URL('PLATFORM_MAIN/config/wapCustomTemplateEdit')}" class="btn btn-sm btn-primary"><i class="fa fa-plus"></i> 添加模板页面</a>
		{if condition="$is_enable == 1"}
			<a  class="btn btn-sm btn-primary js-status" is_enable = "0">关闭自定义模板!(开启中)</a>
		{else/}
			<a  class="btn btn-sm btn-primary js-status" is_enable = "1">开启自定义模板!(关闭中)</a>
		{/if}
		<a  href="javascript:batchDelete();"class="btn btn-sm btn-primary" >批量删除</a>
	</div>
</div>
<section class="panel panel-default">
              <div class="table-responsive">
                <table class="table table-striped b-light text-sm">
                 <colgroup>
					<col width="3%">
					<col width="25%">
					<col width="25%">
					<col width="20%">
					<col width="27%">
				</colgroup>
                  <thead>
                    <tr>
                      <th ><input type="checkbox"></th>
                      <th>模板名称</th>
                      <th style="text-align: center;">创建时间 </th>
                      <th class="" style="text-align: center;">操作</th>
                    </tr>
                  </thead>
                  <tbody id="list">
                    <tr >
				    </tr>
                  </tbody>
                </table>
              </div>
              {include file="platform/page" /}
            </section>

{/block}
{block name="script"}
<script type="text/javascript">
$(function(){
	LoadingInfo(1);
	$(".switch-open").text("启用");
	$(".switch-close").text("禁用");
	
});

	function LoadingInfo(pageIndex) {
// 		var search_text = $("#search_text").val();
		var search_text = '';
		$.ajax({
			type : "post",
			url : "{:__URL('PLATFORM_MAIN/config/wapCustomTemplateList')}",
			async : true,
			data : {
				"pageIndex" : pageIndex, "search_text" : search_text
			},
			success : function(data) {
				var html = '';
				$("#total_count_num").text(data["total_count"]);
				$("#page_count_num").text(data["page_count"]);
				$("#page_count").val(data["page_count"]);
				$("#pageNumber a").remove();
				if (data["data"].length > 0) {
					for (var i = 0; i < data["data"].length; i++) {
						var curr = data['data'][i];
						html += '<tr>';
						html += '<td align="center"><i class="checkbox-common"><input type="checkbox" name="sub" value="' + curr.id + '"></i></td>';
						html += '<td align="left">' + curr.template_name + '</td>';
						html += '<td align="center">' + timeStampTurnTime(curr.create_time) + '</td>';//curr.modify_time
						html += '<td align="center">';
							html += '<a href=\'{:__URL("PLATFORM_MAIN/config/wapCustomTemplateEdit?id=' + curr.id + '")}\'>编辑</a>';
							
							html += '&nbsp;&nbsp;<a href="javascript:deleteCustomTemplateById(' + curr.id + ');">删除</a>';
							if(curr.is_default == 1){
								html += '<span href="javascript:;" style="color:rgb(153, 153, 153);margin-left:8px;">店铺主页</span>';
							}else{
								html += '<a style="margin-left:8px;" href="javascript:setDefaultCustomTemplate(' + curr.id + ');">设为主页</a>';
							}
						html += '</td>';
						html += '</tr>';
						
						
					}
				} else {
					html += '<tr><th colspan="4">暂无记录</th></tr>';
				}
				$("#list").html(html);
				var totalpage = $("#page_count").val();
			if (totalpage == 1) {
				changeClass("all");
			}
			var $html = pagenumShow(jumpNumber,totalpage,{$pageshow})
			$("#pageNumber").append($html);
			}
		});
	}

	function deleteCustomTemplateById(id){

		$( "#dialog" ).dialog({
			buttons: {
				"确定": function() {
					$.ajax({
						type : "post",
						url : "{:__URL('PLATFORM_MAIN/config/deleteWapCustomTemplateById')}",
						data : { "id" : id.toString() },
						success : function(data) {
							if(data["code"] > 0 ){
								LoadingInfo(1);
								showMessage("success",data["message"]);
							}else{
								showMessage("error",data["message"]);
							}
						}
					});
					$(this).dialog('close');
				},
				"取消,#cccccc,#666": function() {
					$(this).dialog('close');
				},
			},
			contentText:"确定要删除吗？",
		});
	}

	//全选
	function CheckAll(event){
		var checked = event.checked;
		$("#list input[type = 'checkbox']").prop("checked",checked);
		if(checked) $("#list input[type = 'checkbox']").parent().addClass("selected");
		else $("#list input[type = 'checkbox']").parent().removeClass("selected");
	}

	/**
	 * 批量删除
	 */
	function batchDelete() {
		var custom_template_id_array= new Array();
		$("#list input[type='checkbox']:checked").each(function() {
			if (!isNaN($(this).val())) {
				custom_template_id_array.push($(this).val());
			}
		});
		if(custom_template_id_array.length ==0){
			$( "#dialog" ).dialog({
				buttons: {
					"确定": function() {
						$(this).dialog('close');
					}
				},
				contentText:"请选择需要操作的记录",
				title:"消息提醒",
			});
			return false;
		}
		deleteCustomTemplateById(custom_template_id_array);
	}

	/**
	 * 设置默认手机端自定义模板
	 */
	function setDefaultCustomTemplate(id){
		$.ajax({
			type : "post",
			url : "{:__URL('PLATFORM_MAIN/config/setDefaultWapCustomTemplate')}",
			data : { "id" : id },
			success : function(res){
				if(res["code"]>0){
					LoadingInfo(1);
					showMessage("success", res["message"]);
				}else{
					showMessage("error",res["message"]);
				}
			}
		});
	}

	function searchData(){
		LoadingInfo(1);
	}

	/**
	 * 开启关闭自定义模板
	 * 创建时间：2017年8月15日 11:19:44 
	 * 更新时间：2017年8月9日 15:27:32
	 */
	$(".js-status").click(function(){
		var is_enable = $(this).attr("is_enable");
		if(is_enable == 1){
			$(this).text("关闭自定义模板!(开启中)");
		}else{
			$(this).text("开启自定义模板!(关闭中)");
		}
		$.ajax({
			type : "post",
			url : "{:__URL('PLATFORM_MAIN/config/setIsEnableWapCustomTemplate')}",
			data : { "is_enable" : is_enable },
			success : function(res){
				if(res.code>0){
					LoadingInfo(1);
					showMessage("success",res.message);
				}else{
					showMessage("error",res.message);
				}
			}
		});
	});
</script>
{/block}